//
//  PHMeViewController.m
//  PhnixHome
//
//  Created by Levy Xu on 2017/11/24.
//  Copyright © 2017年 Levy Xu. All rights reserved.
//

#import "PHMeViewController.h"
#import "PHDevicesViewController.h"
#import "PHInfoViewController.h"
#import "PHAboutViewController.h"

@interface PHMeViewController ()

@end

@implementation PHMeViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self initTableViewHeader];
    [NNCDC addObserver:self selector:@selector(initTableViewHeader) name:kUserInfoChangedNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [NNCDC removeObserver:self name:kUserInfoChangedNotification object:nil];
}

-(void)initTableViewHeader {
    NSURL *url = [NSURL URLWithString:GGlobalUser.info.img];
    
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, APP_WIDTH, 120)];
    header.backgroundColor = UIColorWhite;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 80, 80)];
    imageView.layer.cornerRadius = 40.0f;
    imageView.layer.masksToBounds = YES;
    [imageView sd_setImageWithURL:url placeholderImage:UIImageMake(@"header_default")];
    [header addSubview:imageView];
    
    if (!KIsBlankString(GGlobalUser.info.nick_name)) {
        QMUILabel *label1 = [[QMUILabel alloc] initWithFrame:CGRectMake(120, 35, APP_WIDTH - 120, 20)];
        label1.textColor = GQDThemeManager.currentTheme.themeTintColor;
        label1.text = GGlobalUser.info.nick_name;
        label1.font = CodeFontMake(18);
        [header addSubview:label1];
    }
    CGFloat yoffset = KIsBlankString(GGlobalUser.info.nick_name) ? 50 : 65;
    QMUILabel *label2 = [[QMUILabel alloc] initWithFrame:CGRectMake(120, yoffset, APP_WIDTH - 120, 20)];
    label2.textColor = GQDThemeManager.currentTheme.themeTintColor;
    label2.text = [GGlobalUser.info account];
    label2.font = CodeFontMake(18);
    [header addSubview:label2];
    
    self.tableView.tableHeaderView = header;
}

-(void)initTableView {
    [super initTableView];
    
    QMUIStaticTableViewCellDataSource *dataSource = [[QMUIStaticTableViewCellDataSource alloc] initWithCellDataSections:@[@[
      [self cellWith:0 name:NSLocalizedString(@"settings_infomation", nil) image:@"settings_info"],
      [self cellWith:1 name:NSLocalizedString(@"settings_device_manage", nil) image:@"settings_scene"],
//      [self cellWith:2 name:NSLocalizedString(@"settings_scene_manage", nil) image:@"settings_scene"],
//      [self cellWith:3 name:NSLocalizedString(@"settings_message_manage", nil) image:@"settings_message"],
//      [self cellWith:4 name:NSLocalizedString(@"settings_share_manage", nil) image:@"settings_share"],
//      [self cellWith:5 name:NSLocalizedString(@"settings_question", nil) image:@"settings_question"],
      [self cellWith:6 name:NSLocalizedString(@"settings_about", nil) image:@"settings_about"]
      ]]];
    self.tableView.qmui_staticCellDataSource = dataSource;
}

-(QMUIStaticTableViewCellData *)cellWith:(NSInteger)identifier name:(NSString *)name image:(NSString *)image {
    QMUIStaticTableViewCellData *d = [[QMUIStaticTableViewCellData alloc] init];
    d.identifier = identifier;
    d.style = UITableViewCellStyleDefault;
    d.text = name;
    d.image = UIImageMake(image);
    d.didSelectTarget = self;
    d.didSelectAction = @selector(handleDisclosureIndicatorCellEvent:);
    d.accessoryType = QMUIStaticTableViewCellAccessoryTypeDisclosureIndicator;
    return d;
}

- (void)handleDisclosureIndicatorCellEvent:(QMUIStaticTableViewCellData *)cellData {
    if (cellData.identifier == 0) {
        PHInfoViewController *infoVC = [[PHInfoViewController alloc] initWithStyle:UITableViewStyleGrouped];
        [self.navigationController pushViewController:infoVC animated:YES];
    }
    else if (cellData.identifier == 1) {
        PHDevicesViewController *devVC = [[PHDevicesViewController alloc] initWithStyle:UITableViewStyleGrouped];
        [self.navigationController pushViewController:devVC animated:YES];
    }
    else if (cellData.identifier == 6) {
        PHAboutViewController *aboutVC = [[PHAboutViewController alloc] initWithStyle:UITableViewStyleGrouped];
        [self.navigationController pushViewController:aboutVC animated:YES];
    }
    else {
        [KKHUD showWithText:[NSString stringWithFormat:@"点击了 %@", cellData.text]];
    }
}

- (void)setupNavigationItems {
    [super setupNavigationItems];
    self.title = NSLocalizedString(@"tab_me", nil);
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 1.0f;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return TableViewCellNormalHeight;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView.qmui_staticCellDataSource didSelectRowAtIndexPath:indexPath];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
